﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows.Input;
using SqlMiM.Common.ViewModel;
using SqlMiM.Common.Commands;
using System.Collections.ObjectModel;
using System.Diagnostics;
using SqlMiM.SqlServer.Core;
using System.Windows.Threading;
using System.Collections;
using System.Windows;
using SqlMiM.SqlServer.ViewModel;
using SqlMiM.SqlServer.Model;
using MvvmFoundation.Wpf;


namespace SqlMiM.SqlServer.SpaceUsage
{
    public class ViewModel : ObservableObject
    {
        private RelayCommand selectElements;
        private BackgroundWorker bgw;
        private ObservableCollection<FilesStatView> spaceUsages;



        public ObservableCollection<FilesStatView> SpaceUsages
        {
            get
            {
                return spaceUsages;
            }
            set
            {
                spaceUsages = value;
                this.RaisePropertyChanged("SpaceUsages");
            }
        }

        public ViewModel()
        {
            this.SpaceUsages = new ObservableCollection<FilesStatView>();

            Load();
        }


        public void Load()
        {
            this.SpaceUsages.Clear();

            bgw = new BackgroundWorker();

            bgw.DoWork += (sender, args) => { args.Result = SpaceManagementServices.Current.GetFilesStatistics(); };

            bgw.RunWorkerCompleted += (sender, args) =>
            {
                List<FilesStat> filesStats = args.Result as List<FilesStat>;

                if (filesStats == null) return;

                foreach (var fs in filesStats)
                    this.SpaceUsages.Add(new FilesStatView() { FilesStat = fs });

            };

            bgw.RunWorkerAsync();
        }



    }



}
